<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登陆页面</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <!-- 引入 js -->
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/博客系统logo.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="reg.html">注册</a>
        <a href="login.html">登陆</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="login-container">
        <!-- 中间的登陆框 -->
        <div class="login-dialog">
            <h3>登陆</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username" placeholder="用户名">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password" placeholder="密码">
                <img src="/img/小眼睛-隐藏.png" width="25px" height="25px" id="eye">
            </div>
            <div class="row">
                <span>验证码</span>
                <input type="text" id="code" placeholder="验证码">&nbsp;&nbsp;<img id="codeimg" src=""/>
            </div>
            <div class="row">
                <button id="submit" onclick="mysub()">提交</button>
            </div>
            <a id="forget" href="/chack_username.html">忘记密码</a>
        </div>
    </div>
    
    <script>
        // 页面加载就初始化验证码图片
        $(function() {
            getCode();
        } );

        $("#codeimg").click(function () {
            getCode();
        });
        function getCode() {
            // 本地
            $("#codeimg").attr("src", "http://localhost:8082/user/getcode?" + Math.random() * 10);
            // 服务器
            // $("#codeimg").attr("src", "http://43.139.169.228:8082/user/getcode?" + Math.random() * 10);

        }

        function mysub() {
            // 1. 非空校验
            var username = jQuery("#username");
            var password = jQuery("#password");
            var code = jQuery("#code");
            if (username.val() === "") {
                alert("用户名为空!")
                username.focus(); // 将鼠标光标设置到用户名控件
                return;
            }
            if (password.val() === "") {
                alert("密码为空!")
                password.focus(); // 将鼠标光标设置到密码控件
                return;
            }
            if (code.val() === "") {
                alert("请输入验证码!");
                code.focus();
                return;
            }
            // 2. 发送 ajax 请求
            jQuery.ajax({
                // 请求的地址
                url:"/user/login",
                // 请求类型
                type:"POST",
                // 请求的参数
                data:{"username": username.val(), "password": password.val(), "code": code.val()},
                // 回调函数
                success:function(result) {
                    // 响应的结果
                    if (result != null && result.code === 200 && result.data != null) {
                        // 登录成功
                        location.href = "myblog_list.html";
                    } else if (result != null && result.code === -6 && result.data == null) {
                        alert("验证码错误!");
                        location.href = location.href;
                    }else if (result != null && result.code === -2 && result.data == null) {
                        alert("该账号已被冻结!");
                    } else if (result != null && result.code === -7 && result.data == null) {
                        alert("当前账号未注册!");
                    } else {
                        alert("登录失败, 账号或密码不正确!请重试!您还有" + result.msg + "次机会");
                    }
                }
            });
        }

        // 密码小眼睛
        var eye = document.getElementById("eye");
        var pwd = document.getElementById("password");
        var flag = 0; // 0 表示隐藏密码, 1 表示显示密码
        eye.onclick = function() {
            if (flag == 0) {
                pwd.type = "text";
                eye.src = "/img/小眼睛-01.png";
                flag = 1;
            } else {
                pwd.type = "password";
                eye.src = "/img/小眼睛-隐藏.png";
                flag = 0;
            }
        }
    </script>
</body>

</html>